package com.wei.wiki.controller;

/*
 * @author : ShunWei
 * @date : 2022/1/27 17:20
 * @Description:
 */

import com.wei.wiki.req.DocQueryReq;
import com.wei.wiki.req.DocSaveReq;
import com.wei.wiki.resp.DocQueryResp;
import com.wei.wiki.resp.CommonResp;
import com.wei.wiki.resp.PageResp;
import com.wei.wiki.service.DocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/doc")
@Api(tags = "DocController", description = "文档控制类")
public class DocController {
    private DocService docService;

    @Autowired
    public void setDocService(DocService docService) {
        this.docService = docService;
    }

    @GetMapping("/all/{ebookId}")
    @ApiOperation("文档列表")
    public CommonResp getAllDocList(@PathVariable Long ebookId) {
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> docList = docService.all(ebookId);
        resp.setSuccess(true);
        resp.setContent(docList);
        resp.setMessage("success");
        return resp;
    }

    @GetMapping("/list")
    @ApiOperation("文档列表 分页")
    public CommonResp getDocList(@Valid DocQueryReq req) {
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        PageResp<DocQueryResp> docList = docService.docList(req);
        resp.setSuccess(true);
        resp.setContent(docList);
        resp.setMessage("success");
        return resp;
    }

    @PostMapping("/save")
    @ApiOperation("新增、修改文档")
    public CommonResp save(@RequestBody @ApiParam("文档对象") DocSaveReq req) {
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        resp.setSuccess(true);
        resp.setMessage("success");
        return resp;
    }

    @DeleteMapping("/delete/{idsStr}")
    @ApiOperation("根据id删除文档")
    public CommonResp delete(@PathVariable String idsStr) {
        CommonResp resp = new CommonResp<>();
        List<String> list = Arrays.asList(idsStr.split(","));
        docService.delete(list);
        return resp;
    }

    @GetMapping("/find-content/{id}")
    @ApiOperation("根据id查找内容")
    public CommonResp findContentById(@PathVariable Long id) {
        CommonResp<String> resp = new CommonResp<>();
        String content = docService.findContentById(id);
        resp.setContent(content);
        return resp;
    }

    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable Long id) {
        CommonResp resp = new CommonResp();
        docService.vote(id);
        return resp;
    }
}
